home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Info / TeachU14 / SAMS / Code / Day09 / spmain.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-08  |  5.8 KB  |  193 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl\vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "SPMain.h"
  6. #include "SPAbout.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10. TScratchPad *ScratchPad;
  11. //---------------------------------------------------------------------------
  12. __fastcall TScratchPad::TScratchPad(TComponent* Owner)
  13.   : TForm(Owner)
  14. {
  15. }
  16. //---------------------------------------------------------------------------
  17. void __fastcall TScratchPad::FileExitClick(TObject *Sender)
  18. {
  19.   //
  20.   // All done. Close the form.
  21.   //
  22.   Close();  
  23. }
  24. //---------------------------------------------------------------------------
  25. void __fastcall TScratchPad::EditCutClick(TObject *Sender)
  26. {
  27.   //
  28.   // Call TMemo::CutToClipboard().
  29.   //
  30.   Memo->CutToClipboard();
  31. }
  32. //---------------------------------------------------------------------------
  33. void __fastcall TScratchPad::EditCopyClick(TObject *Sender)
  34. {
  35.   //
  36.   // Call TMemo::CopyToClipboard().
  37.   //
  38.   Memo->CopyToClipboard();
  39. }
  40. //---------------------------------------------------------------------------
  41. void __fastcall TScratchPad::EditPasteClick(TObject *Sender)
  42. {
  43.   //
  44.   // Call TMemo::PasteFromClipboard().
  45.   //
  46.   Memo->PasteFromClipboard();
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall TScratchPad::FileNewClick(TObject *Sender)
  50. {
  51.   //
  52.   // Open a file. First check to see if the current file
  53.   // needs to be saved.
  54.   //
  55.   if (Memo->Modified) {
  56.     //
  57.     // Display a message box.
  58.     //
  59.     int result = Application->MessageBox(
  60.       "The current file has changed. Save changes?",
  61.       "ScratchPad Message", MB_YESNOCANCEL);
  62.     //
  63.     // If Yes was clicked then save the current file.
  64.     //
  65.      if (result == IDYES) FileSaveClick(Sender);
  66.     //
  67.     // If No was clicked then do nothing.
  68.     //
  69.     if (result == IDCANCEL) return;
  70.   }
  71.   //
  72.   // Delete the strings in the memo, if any.
  73.   //
  74.   if (Memo->Lines->Count > 0) Memo->Clear();
  75.   //
  76.   // Set the FileName property of the Save Dialog to a
  77.   // blank string. This lets us know that the file has
  78.   // not yet been saved.
  79.   //
  80.   SaveDialog->FileName = "";
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TScratchPad::FileOpenClick(TObject *Sender)
  84. {
  85.   //
  86.   // Open a file. First check to see if the current file needs
  87.   // to be saved. Same logic as in FileNewClick() above.
  88.   //
  89.   if (Memo->Modified) {
  90.     int result = Application->MessageBox(
  91.       "The current file has changed. Save changes?",
  92.       "ScratchPad Message", MB_YESNOCANCEL);
  93.      if (result == IDYES) FileSaveClick(0);
  94.     if (result == IDCANCEL) return;
  95.   }
  96.   //
  97.   // Execute the File Open dialog. If OK was pressed then
  98.   // open the file using the LoadFromFile() method. First
  99.   // clear the FileName property.
  100.   //
  101.   OpenDialog->FileName = "";
  102.   if (OpenDialog->Execute())
  103.   {
  104.     if (Memo->Lines->Count > 0) Memo->Clear();
  105.      Memo->Lines->LoadFromFile(OpenDialog->FileName);
  106.     SaveDialog->FileName = OpenDialog->FileName;
  107.   }
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TScratchPad::FileSaveClick(TObject *Sender)
  111. {
  112.   //
  113.   // If a filename has already been provided then there is
  114.   // no need to bring up the File Save dialog. Just save the
  115.   // file using SaveToFile().
  116.   //
  117.   if (SaveDialog->FileName != "")
  118.   {
  119.     Memo->Lines->SaveToFile(SaveDialog->FileName);
  120.     //
  121.     // Set Modified to false since we've just saved.
  122.     //
  123.     Memo->Modified = false;
  124.   }
  125.   //
  126.   // If no filename was set then do a SaveAs().
  127.   //
  128.   else FileSaveAsClick(Sender);
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall TScratchPad::FileSaveAsClick(TObject *Sender)
  132. {
  133.   //
  134.   // Display the File Save dialog to save the file.
  135.   // Set Modified to false since we just saved.
  136.   //
  137.   SaveDialog->Title = "Save As";
  138.   if (SaveDialog->Execute())
  139.   {
  140.     Memo->Lines->SaveToFile(SaveDialog->FileName);
  141.     Memo->Modified = false;
  142.   }
  143. }
  144. //---------------------------------------------------------------------------
  145. void __fastcall TScratchPad::EditUndoClick(TObject *Sender)
  146. {
  147.   //
  148.   // TMemo doesn't have an Undo method so we have to send
  149.   // a Windows WM_UNDO message to the memo component.
  150.   //
  151.   SendMessage(Memo->Handle, WM_UNDO, 0, 0);
  152. }
  153. //---------------------------------------------------------------------------
  154. void __fastcall TScratchPad::EditSelectAllClick(TObject *Sender)
  155. {
  156.   //
  157.   // Just call TMemo::SelectAll().
  158.   //
  159.   Memo->SelectAll();
  160. }
  161. //---------------------------------------------------------------------------
  162. void __fastcall TScratchPad::EditWordWrapClick(TObject *Sender)
  163. {
  164.   //
  165.   // Toggle the TMemo::WordWrap property. Set the Checked
  166.   // property of the menu item to the same value as WordWrap.
  167.   //
  168.   Memo->WordWrap = !Memo->WordWrap;
  169.   EditWordWrap->Checked = Memo->WordWrap;
  170.   //
  171.   // If WordWrap is on then we only need the vertical scroll
  172.   // bar. If it's off, then we need both scroll bars.
  173.   //
  174.   if (Memo->WordWrap) Memo->ScrollBars = ssVertical;
  175.   else Memo->ScrollBars = ssBoth;
  176. }
  177. //---------------------------------------------------------------------------
  178. void __fastcall TScratchPad::OnHint(TObject* Sender)
  179. {
  180.   StatusBar->SimpleText = Application->Hint;
  181. }
  182. void __fastcall TScratchPad::FormCreate(TObject *Sender)
  183. {
  184.   Application->OnHint = OnHint;  
  185. }
  186. //---------------------------------------------------------------------------
  187. void __fastcall TScratchPad::HelpAboutClick(TObject *Sender)
  188. {
  189.   AboutBox->ShowModal();  
  190. }
  191. //---------------------------------------------------------------------------
  192.  
  193.